
<template>
    <vo-table ref="yuTable" :data="data.tableData.data" :cols="data.tableData.cols" :config="data.tableData.config" @fetch-data="query" @vue:mounted="query"  >
      <template #leftBar>
        <tiny-input v-model="data.searchData.jobId" placeholder="任务ID"></tiny-input>
        <tiny-input v-model="data.searchData.keyword" placeholder="关键字"></tiny-input>
        <tiny-button  size="small" ghost type="info" @click="query"  ><vo-icon iconName="sousuo"  ></vo-icon>查询</tiny-button>
        </template>
        <template #rightBar>
            <tiny-button-group class="ml-4">
            <tiny-button ghost type="success" size="small"  @click="addDict"><vo-icon iconName="add" ></vo-icon>导入任务</tiny-button>
            <tiny-button ghost type="warning" size="small" @click="addTask"><vo-icon iconName="add" ></vo-icon>新建任务</tiny-button>
        </tiny-button-group>
        </template>

        <template v-slot:statusSolt="{row}">
            <tiny-switch v-model="row.enable"></tiny-switch>
      </template>

        <template v-slot:handleSlot="{row}">
            <tiny-action-menu :options="data.buttons" 
            :suffix-icon="tinyIconEllipsis"
             spacing="3px"
             trigger="click"
            :max-show-num="2"
            @item-click="itemClick(row, $event)"
            @more-click="moreClick"> </tiny-action-menu>
      </template>
           
    </vo-table>

    <tiny-dialog-box   v-model:visible="data.params.isShow" title="任务参数" width="40%">
      <tiny-input v-model="data.params.params" type="textarea" placeholder="JSON参数" :rows="5" ></tiny-input>
      <template #footer>
        <tiny-button type="primary" @click="runTaskByParams"> 确定 </tiny-button>
        <tiny-button @click="data.params.isShow = false"> 取消 </tiny-button>
      </template>
    </tiny-dialog-box>

    <tiny-dialog-box   v-model:visible="data.export.isShow" title="导出数据" width="40%">
      <!-- <tiny-input v-model="data.export.params" type="textarea"  :rows="5" ></tiny-input> -->
      <yu-ace-editor :data="data.export.params" lang="json" v-model:value="data.export.params" @handleCells="handleCells" @handleFormat="handleFormat"></yu-ace-editor>
      <template #footer>
        <tiny-button type="primary" @click="copyBtn"> 确定 </tiny-button>
        <tiny-button @click="data.params.isShow = false"> 取消 </tiny-button>
      </template>
    </tiny-dialog-box>


    <vo-dialog :data="data.editData.dialogConfig"  v-model:isShow="data.editData.isShow">
      <PowerJobTaskEdit  :data="data.editData.data" v-model:isShow="data.editData.isShow" ></PowerJobTaskEdit>
    </vo-dialog>

</template>
<script setup>
  import {reactive, ref, getCurrentInstance,onMounted} from 'vue';  
  import { TinySwitch ,TinyActionMenu ,Modal,TinyNotify ,TinyDialogBox } from '@opentiny/vue'
  import PowerJobTaskEdit from './PowerJobTaskEdit.vue';
  import {cosSystem} from '@/stores/system.js';
  let store = cosSystem();
  const yuTable = ref(null);
  const {proxy} = getCurrentInstance();
onMounted(()=>{
    if(!store.getAppId){
      Modal.message({ message: '请先选择一个应用', status: 'warning' })
      proxy.$router.push({path:'/sys/powerJob/app'});
    }
    data.searchData.appId = store.getAppId;
})

  const data = reactive({
    export:{isShow:false,params:"{}"},
    copy:{isShow:false,params:{}},
    params:{isShow:false,params:""},
    tableData: {  data: {}, cols: [],  config: {} },
    searchData: {  appId: '1',  jobId: '',  keyword: '',index: 0,pageSize: 10},
    isEdit:false,
    editData:{data:{},dialogConfig:{title: '', width: '1200px'},isShow:false},
    buttons:[
        {label:'编辑',name:'edit'},
        {label:'运行',name:'run'},
        {label:'运行参数',name:'runParam'},
        {label:'运行记录',name:'runLog'},
        {label:'复制',name:'copy'},
        {label:'导出',name:'export'},
        {label:'删除',name:'delete'},
    ],
  })
  data.tableData.data = [{dictCode: '123', dictName: '123'}]
  data.tableData.cols = [
    {type: 'index',title: '序号'},
    {title: '任务ID', key: 'id', align: 'left'},
    {title: '任务名称', key: 'jobName', align: 'left'},
    {title: '定时信息', key: 'timeExpressionType', align: 'left'},
    {title: '执行类型', key: 'processorType', align: 'left'},
    {title: '处理器类型', key: 'executeType', align: 'left'},
    {title: '状态', key: 'enable', align: 'left',slot: 'statusSolt'},
    { title: '操作', align: 'center', slot: 'handleSlot',  },
]
//参数
const query = ()=>{
  proxy.$api.taskJob.jobTaskList(data.searchData).then(res=>{
     data.tableData.data = res

  })
}
// 新增任务
const addTask = ()=>{
    data.editData.dialogConfig.title='新增任务';
    data.editData.data={
      enable:false,
        maxInstanceNum:0,
        concurrency:5,
        instanceTimeLimit:0,
        instanceRetryNum:0,
        taskRetryNum:1,
        minCpuCores:0,
        minMemorySpace:0,
        minDiskSpace:0,
        maxWorkerCount:0,
        lifeCycle:{start:"",end:""},
        alarmConfig:{
            alertThreshold:0,
            statisticWindowLen:0,
            silenceWindowLen:0, 
        },
        logConfig:{
            type:"",
            level:"",
            loggerName:""
        },
        advancedRuntimeConfig:{
            taskTrackerBehavior:""
        }
    };
    data.editData.isShow=true;
}
// 操作
const itemClick = (row,item)=>{
  switch(item.itemData.name){
      case 'edit':
          editRow(row);
          break;
      case 'run':
          runTask(row);
          break;
      case 'runParam':
          runTaskParams(row);
          break;
      case 'copy':
          copyRow(row);
          break;
      case 'export':
          exportRow(row);
          break;
      case 'runLog':
          runLog(row);
          break;
      case 'delete':
          deleteRow(row);
          break;
  }
}
// 编辑任务
const editRow = (row)=>{
  data.editData.dialogConfig.title='编辑任务';
  data.editData.data=JSON.parse(JSON.stringify(row));
  data.editData.isShow=true; 
}
// 复制任务
const copyRow = (row)=>{
  console.log(row)
  data.editData.dialogConfig.title='复制任务';
  let params = {
    jobId:row.id
  } 
  proxy.$api.taskJob.copy(params).then(res=>{
    if(res.success){
        data.editData.isShow=true; 
        data.editData.data=JSON.parse(JSON.stringify(res.data));
      }
  })
}
// 运行日志
const runLog = (row)=>{
  console.log("运行日志")
  proxy.$router.push({path:'/sys/powerJob/instance',query:{jobId:row.id}});
}
//删除任务
const deleteRow = (row)=>{
  let params = {
    jobId:row.id
  } 
  Modal.confirm('此操作将永久删除任务, 是否继续?').then(res => {
    if (res == 'confirm') {
      proxy.$api.taskJob.deleteJob(params).then(res=>{
        if(res.success){
          Modal.message({status: 'success', message: "删除成功"});
            query();
          }else{
            Modal.message({status:'error', message: "删除失败"});
          }
      })
    }
  })
  

}
// 导出参数
const exportRow = (row)=>{
  let params = {
    jobId:row.id
  } 
  proxy.$api.taskJob.export(params).then(res=>{
    data.export.isShow=true;
    data.export.params=JSON.stringify(res.data, null, 2 );
  })
}
// 运行弹窗
const runTaskParams=(row)=>{
  data.params.isShow=true;
  data.params.row=row;
}
// 运行参数
const runTaskByParams = ()=>{
  let params = {
    appId:data.params.row.appId,
    jobId:data.params.row.id,
    instanceParams:encodeURIComponent(data.params.params)
  } 
  proxy.$api.taskJob.runJobTask(params).then(res=>{
    console.log(res)
    if(res.success){
      data.params.isShow=false;
      Modal.message({status:'success', message: "运行成功"}); 
    }else{
      Modal.message({status:'error', message: "运行失败,请检查参数是否正确"});
    }
  })

}
// 运行任务
const runTask = (row)=>{
  let params={appId:row.appId,jobId:row.id}
  proxy.$api.taskJob.runJobTask(params).then(res=>{
    console.log(res)
    if(res.success){
      Modal.message({status: 'success', message: "运行成功"});
    }else{
      Modal.message({status:'error', message: "运行失败"});
    }
  })
}
</script>

<style lang="scss" scoped>



</style>